package com.easyfun.healthmagicbox.comm;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.content.Context;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.UserJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.User;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;

public class UserRegisterTask extends BaseCommTask implements Runnable,
DatabaseAccess {
	
	User user;
	
	public UserRegisterTask(Context context, User user2, CommCallback callback) {

		super(context,callback);
		user = user2;
	}
	@Override
	public void run() {
		UserJsonWrapper<?> wrapper = new UserJsonWrapper(
				user);

		Map<String, String> parmas = new HashMap<String, String>();
		String result="";
		try {
		     parmas.put("jsonstring", wrapper.serialize());
			result = super.uploadByHTTP(parmas,ConstantData.UserRegisterURI);
			if (ServerResponseCode.parseOf(result)==ServerResponseCode.SUCCESS)
			{
				ccb.setCommResult(this, 
						result, ServerResponseCode.SUCCESS);
				return;
			}else if (ServerResponseCode.parseOf(result)==ServerResponseCode.NOMORE)
			{
				ccb.setCommResult(this, 
					result, ServerResponseCode.NOMORE);
				return;
			}else
			{
				ccb.setCommResult(this, 
						result, ServerResponseCode.FAILED);
				return;
			}
			
		} catch (Exception e) {
			e.printStackTrace();

			ccb.setCommResult(this, 
					result, ServerResponseCode.FAILED);
		}
	}
	
	@Override
	public DatabaseHelper getHelper(Context context) {

		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}

}
